<?php
// Obtener contactos activos
$id = Yii::app()->user->id;
$numItem = $modelCampaniaOwner->searchContactos($model->id)->itemCount;
$usersCampania = new CampaniaUsuarioCruge;
$userItem = $usersCampania->searchUsuarioCampania($model->id)->itemCount;
$accionesCampania = Campania::model()->campaniaAccionCuentas($model->id);
$cuentasSinOpe = $modelCampaniaOwner->count('(owner_id=:owner_id or owner_id is null) AND campania_id=:campania_id ', array('owner_id' => 0, 'campania_id' => $model->id));
$permitidoaccionesfecha = $model->permitirBotonesFecha();
//acciones de la campania
$acciones = CampaniaAccion::model()->getAccionesCampania($model->id);
$buttons = array(
    'delete' => array(
        'label' => '"<button class=\"btn btn-danger\"><i class=\"icon-trash\"></i></button>"',
        'options' => array('title' => 'Eliminar'),
        'url' => 'Yii::app()->createUrl("campanias/campaniaClientesOwner/delete", array ("id" => $data->id)) ',
        'imageUrl' => true,
    ),
);
$columns = array(
    array(
        'class' => 'CCheckBoxColumn',
    ),
    array(
        'header' => 'Ident.',
        'name' => 'identificacion',
        'value' => '$data->identificacion',
        'type' => 'raw',
    ),
    array(
        'name' => 'cliente_id',
        'value' => '$data->cliente_id',
        'type' => 'raw',
    ),
    array(
        'name' => 'owner_id',
        'value' => '$data->owner_id',
        'type' => 'raw',
    ),
);
$template = '{delete}';

foreach ($accionesCampania as $key => $value) {
    switch ($value) {
        case 'LLAMADA':
            array_push($columns, array(
                'header' => 'Call',
                'name' => 'cantidad_llamada',
                'value' => '($data->telefono||$data->celular)?$data->generatebuton("LLAMADA"):""',
                'type' => 'raw',
            ));

            break;
        case 'EMAIL':
            array_push($columns, array(
                'header' => 'E-mail',
                'name' => 'cantidad_mail',
                'value' => '($data->email_1||$data->email_2)?$data->generatebuton("EMAIL"):""',
                'type' => 'raw',
            ));

            break;
        case 'SMS':
            array_push($columns, array(
                'header' => 'Sms',
                'name' => 'cantidad_sms',
                'value' => '($data->celular)?$data->generatebuton("SMS"):""',
                'type' => 'raw',
            ));

            break;
    }
}

array_push($columns, array(
    'class' => 'ext.bootstrap.widgets.TbButtonColumnV1',
    'template' => $template,
    'buttons' => $buttons,
    'cssClassExpression' => '',
    'htmlOptions' => array(
        'width' => '30px'
    ),
    'visible' => 'false'
));
//die(var_dump($columns));
?>
<div class="widget green">
    <div class="widget-title">
        <h4><i class="icon-briefcase"></i> Clientes </h4>
        <span class="tools">
            <a href="javascript:;" class="icon-chevron-down"></a>
            <a href="javascript:;" class="icon-remove"></a>
        </span>
    </div>
    <div class="widget-body">
        <div class="row-fluid">
            <div class='grid <?php echo($numItem > 0) ? '' : 'hidden'; ?>'>
                <div class="btn-toolbar">
                    <div class="btn-group">
                        <button id="btn_asignar_clienets" data-toggle="dropdown" class="btn btn-small btn-inverse dropdown-toggle" type="button">Asignar clientes<span class="caret"></span></button>
                        <ul class="dropdown-menu">
                            <li><a id="li_asignar_todos"  href="#">Todos</a></li>
                            <li><a id="li_asignar_seleccionados" href="#">Seleccionados</a></li>
                            <li class="<?php echo ($userItem > 0 && $cuentasSinOpe > 0) ? '' : 'hidden'; ?>"><a id="li_asignar_auto" href="#">Automática</a></li>
                        </ul>
                    </div>
                    <div id="divAcctionSMS"  class="btn-group accion_cliente <?php echo in_array('SMS', $acciones) && $permitidoaccionesfecha ? '' : 'hidden'; ?>">
                        <button data-toggle="dropdown" class="btn btn-small btn-success  dropdown-toggle">SMS <span class="caret"></span></button>
                        <ul class="dropdown-menu">
                            <li><a id="li_sms_todos"  href="#">Todos</a></li>
                            <li><a id="li_sms_seleccionados"  href="#">Seleccionados</a></li>
                        </ul>
                    </div>
                    <div id="divAcctionEMAIL" class="btn-group accion_cliente <?php echo in_array('EMAIL' && $permitidoaccionesfecha, $acciones) ? '' : 'hidden'; ?>">
                        <button data-toggle="dropdown" class="btn btn-small btn-primary  dropdown-toggle">EMAIL <span class="caret"></span></button>
                        <ul class="dropdown-menu">
                            <li><a id="li_email_todos" href="#">Todos</a></li>
                            <li><a id="li_email_seleccionados" href="#">Seleccionados</a></li>
                        </ul>
                    </div>
                </div>

                <?php
                $this->widget('ext.Truulo.TruuloModuleSearch', array(
                    'model' => $modelCampaniaOwner,
                    'grid_id' => 'cuenta-grid',
                    'id' => 'form_view',
                    'nameInput' => 'input_view',
                    'idList' => 'sel_view',
                        )
                );
                ?>
            </div>
            <div style='overflow:auto'  class='grid <?php echo($numItem > 0) ? '' : 'hidden'; ?>'> 
                <?php
                $this->widget('ext.selgridview.BootSelGridView', array(
                    'id' => 'cuenta-grid',
                    "template" => "{items}{summary}{pager}",
                    'showTableOnEmpty' => false,
                    'afterAjaxUpdate' => "function(id,data){activarAcciones()}",
                    'type' => 'striped bordered hover advance',
                    'dataProvider' => $modelCampaniaOwner->searchContactosCampania($model->id),
                    'selectableRows' => 2,
                    'columns' => $columns,
                ));
                ?>
            </div> 
            <br>

            <?php
            Util::checkAccess(array('action_contacto_createModal')) ?
                            $this->widget(
                                    'bootstrap.widgets.TbButton', array(
                                'id' => 'icon-briefcase',
                                'label' => (($numItem > 0) ? '' : '<br>') . 'Agregar Contactos',
                                'encodeLabel' => false,
                                'icon' => ($numItem > 0) ? 'plus-sign' : 'user',
                                'htmlOptions' => array(
                                    'onClick' => 'js:viewModal("campanias/campaniaClientesOwner/create/id_campania/' . $model->id . '",function(){'
                                    . 'maskAttributes();})',
                                    'class' => ($numItem > 0) ? '' : 'empty-portlet',
                                ),
                                    )
                            ) : '';
            ?>           
        </div>
    </div>
</div>

